package RobotPlayer
{
	import BasicEngine.Dispatcher;
	import BasicEngine.Vector2D;
	import DataStore.BmpDataPool;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	/**
	 * 机器零件.
	 * 各个机器零件组成模块
	 * 零件的位置是相对于部件而言.
	 */
	public class RobotPart 
	{
		/**
		 * 零件名称
		 */
		public var Name:String;
		/**
		 * 零件编号
		 */
		public var Idnum:int;
		/**
		 * 安装位置
		 */
		public var Center:Point;
		/**
		 * 朝向
		 */
		public var Heading:Vector2D;
		/**
		 * 零件显示
		 */
		public var  Img:Bitmap;
		/**
		 * 零件容器
		 */
		public var Canvas:Sprite;
		/**
		 * 状态
		 */		
		public var States:Dictionary;
		/**
		 * 当前状态名称
		 */
		public var CurrentStateName:String;
		/**
		 * 是否停止动画
		 */
		public var StopAnimation:Boolean = false;
		/**
		 * 一次性动画停止标志
		 */
		public var OnceStopAnimation:Boolean = false;
		/**
		 * 当前状态
		 */
		private var currentState:AnimationState;
		/**
		 * 私有计时器
		 */
		private var timer:int = 0;
		
		private var index:int = 0;

		/**
		 * _name:零件名称 _defaultHeading:Number 默认角度(角度单位) _x:int 偏移X位置 _y:int 偏移Y位置 _scale:Number 缩放比（默认1.0）
		 */
		public function RobotPart(_name:String, _x:int = 0,_y:int = 0,_scale:Number = 1.0)
		{
			Heading = new Vector2D(1,1);
			Heading.Angle = 0;
			Img = new Bitmap();
			Canvas = new Sprite();
			States = new Dictionary();
			Name = _name;
			Canvas.addChild(Img);
			Img.x = _x;
			Img.y = _y;
			
			
			var shape:Shape = new Shape();
			shape.graphics.beginFill(0xff0000, 1);
			shape.graphics.drawCircle(0, 0, 2);
			Canvas.addChild(shape);
			Canvas.scaleY = _scale;
			Canvas.scaleX = _scale;
			
			Canvas.addEventListener(Event.ENTER_FRAME, function(e:Event):void
			{
				Update();
			});
		}
		
		/**
		 * 此零件只有一帧（或者一种动画状态）
		 */
		public function SetStatic(_array:Array,_delay:int):void
		{
			var state:AnimationState = new AnimationState("_default", _delay,_array);
			this.AddState(state,true);
		}
		
	
		public function ColorTransform():void
		{	
			var matrix:Array = new Array();
            matrix = matrix.concat([0, 0, 0, 0, 0]); // red
            matrix = matrix.concat([0, 0, 0, 0, 0]); // green
            matrix = matrix.concat([0, 0, 1, 0, 0]); // blue
            matrix = matrix.concat([0, 0, 0, 1, 0]); // alpha
			
            var filter:ColorMatrixFilter = new ColorMatrixFilter(matrix);
            var filters:Array = new Array();
            filters.push(filter);
            Img.filters = filters;
        }
		
		/**
		 * _name:String 名称 _delay:int 间隔时间 _array:Array 索引 _isdefault:Boolean 默认状态 _once:Boolean 一次性动作）
		 */
		public function AddStateData(_name:String, _delay:int, _array:Array, _isdefault:Boolean = false,_once:Boolean = false):void
		{
			var state:AnimationState = new AnimationState(_name, _delay, _array,_once);
			AddState(state, _isdefault);
		}
		
		public function AddState(_f:AnimationState, _isdefault:Boolean):void
		{
			States[_f.Name] = _f;
			if (_isdefault)
			{
			   SetState(_f.Name);
			}
		}
		
		public function SetState(_name:String):void
		{
			if (_name != CurrentStateName)
			{
			   CurrentStateName = _name;
			   currentState = States[_name];
			   index = 0;
			}
		}
		
		public function Dispose():void
		{
			Img.bitmapData = null;
			States = null;
		}
		
		private function Update():void
		{
			if (Dispatcher.Clock.SystemTime - timer > currentState.Delay)
			{
				if (index >= currentState.Queue.length -1)
				{
			        if (!currentState.Once)
					{
					   index = 0;
					}
					else
					{
						StopAnimation = true;
						index = 0;
					}
				}
				var t:Dictionary = BmpDataPool.RobotPool;
				Img.bitmapData = BmpDataPool.RobotPool[currentState.Queue[index]];
				timer = Dispatcher.Clock.SystemTime;
				Canvas.rotation =  Heading.Angle * 180 / Math.PI;
				
				if (!StopAnimation)
				{
				   index++;
				}
				else
				{
					index = 0;
				}
			}
		}
	}
}